package com.snack.manage.tag;

import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import com.lamfire.utils.StringUtils;
import com.snack.manage.util.Tool;

public class PageTag extends TagSupport {

	private static final long serialVersionUID = 263486137025259435L;
	private static final int blockIndex = 10; // 显示多少页

	private long itemCount = 0; // 总记录数
	private long pageIndex = 1; // 当前页
	private int pageSize = 10; // 页面容量
	private int pageCount; // 总页数

	private String action; // 请求数据的地址
	private String conditions; // 查询条件
	private String queryValues; // 和查询条件对应的值
	private String indexClass = "act"; // 当前页的样式

	@Override
	public int doStartTag() {
		try {
			pageCount = this.getPageCount(); // 总页数
			// 如果总记录数小于等于零，不显示分页标签
			if (itemCount < 1) {
				return Tag.EVAL_BODY_INCLUDE;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return Tag.SKIP_BODY;

	}

	@Override
	public int doEndTag() {
		String html = createHtml();
		// 输出显示分页的代码
		try {
			// 输出分页创建的form
			pageContext.getOut().println(html);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 表示按照正常的流程继续执行JSP网页
		return EVAL_PAGE;

	}

	/**
	 * 创建标签
	 * 
	 * @return
	 */
	private String createHtml() {
		StringBuffer result = new StringBuffer();
		StringBuffer conditionsQuery = new StringBuffer();

		try {
			// 如果有查询参数，加入参数
			if (StringUtils.isNotBlank(conditions)
					&& StringUtils.isNotBlank(queryValues)) {
				String[] names = conditions.split(",");
				String[] values = queryValues.split(",");
				int names_lenght = names.length;
				int values_lenght = values.length;
				if (names_lenght > 0 && values_lenght > 0) {
					for (int i = 0, length = names.length; i < length; i++) {
						if (i > names_lenght - 1 || i > values_lenght - 1) {
							break;
						}

						String name = names[i];
						String value = values[i];
						if (StringUtils.isEmpty(name)
								|| StringUtils.isEmpty(value)) {
							break;
						}

						if (i > 0) {
							conditionsQuery.append("&");
						}

						conditionsQuery.append(name);
						conditionsQuery.append("=");

						if (Tool.isContainsChinese(value)) {
							conditionsQuery.append(Tool.encoder(value));
						} else {
							conditionsQuery.append(value);
						}
					}
				}
			}
			// 基础查询
			String query = action + "?" + conditionsQuery;
			String nowQuery = null;
			if (query.lastIndexOf("?") < (query.length() - 1)) {
				query += "&";
			}

			int prePage = (int) (pageIndex - 1); // 上一页
			int nextPage = (int) (pageIndex + 1); // 下一页

			int nowIndex = 0;
			if (pageIndex > 1) {
				nowIndex = 1;
				nowQuery = query + "pageIndex=" + nowIndex;
				result.append("<a href=\"" + nowQuery + "\">");
				result.append("首&nbsp;页");
				result.append("</a>");

			} else {
				result.append("<a href=\"javascript:\">");
				result.append("首&nbsp;页");
				result.append("</a>");
			}

			if (prePage > 0) {
				nowIndex = prePage;
				nowQuery = query + "pageIndex=" + nowIndex;
				result.append("<a href=\"" + nowQuery + "\">");
				result.append("上一页");
				result.append("</a>");
			} else {
				result.append("<a href=\"javascript:\" >上一页</a>");
			}

			// 判断显示的页面
			if ((pageIndex + 1) >= blockIndex) {
				int start = (int) (pageIndex - blockIndex / 2);
				if (start < 1) {
					start = 1;
				}
				int end = (int) (pageIndex + blockIndex / 2);
				if (end > pageCount) {
					end = pageCount;
				}
				while (start <= end) {
					nowIndex = start;
					nowQuery = query + "pageIndex=" + nowIndex;
					if (pageIndex == nowIndex) {
						result.append("<a href=\"" + nowQuery + "\" class=\""
								+ indexClass + "\" >" + start + "</a>");
					} else {
						result.append("<a href=\"" + nowQuery + "\" >" + start
								+ "</a>");
					}
					start++;
				}
			} else {
				int i = 1;
				int end = blockIndex;
				if (blockIndex > pageCount) {
					end = pageCount;
				}
				while (i <= end) {
					nowIndex = i;
					nowQuery = query + "pageIndex=" + nowIndex;
					if (pageIndex == nowIndex) {
						result.append("<a href=\"" + nowQuery + "\" class=\""
								+ indexClass + "\" >" + i + "</a>");
					} else {
						result.append("<a href=\"" + nowQuery + "\" >" + i
								+ "</a>");
					}
					i++;
				}
			}

			if (nextPage <= this.pageCount) {
				nowIndex = nextPage;
				nowQuery = query + "pageIndex=" + nowIndex;
				result.append("<a href=\"" + nowQuery + "\">");
				result.append("下一页");
				result.append("</a>");
			} else {
				result.append("<a href=\"javascript:\" >下一页</a>");
			}

			if (pageIndex < pageCount) {
				nowIndex = pageCount;
				nowQuery = query + "pageIndex=" + nowIndex;
				result.append("<a href=\"" + nowQuery + "\">");
				result.append("尾&nbsp;页");
				result.append("</a>");

				nowQuery = null;
			} else {
				result.append("<a href=\"javascript:\">");
				result.append("尾&nbsp;页");
				result.append("</a>");
			}
			result.append("&nbsp;&nbsp;");
			if (pageIndex < 1) {
				pageIndex = 1;
			}
			if (pageIndex > pageCount) {
				pageIndex = pageCount;
			}
			result.append("" + pageIndex + "/" + pageCount + "&nbsp;");
			result.append("&nbsp;&nbsp;");
			result.append("(" + itemCount + ")");
			result.append("&nbsp;&nbsp;");

			result.append("<input type=\"text\" id=\"pageIndex\" name=\"pageIndex\" style=\"width:30px;\" />");
			result.append("&nbsp;&nbsp;");
			result.append("<a id=\"a_go\" href=\"javascript:\" onclick=\"goPage('"
					+ pageCount + "','" + query + "');\">GO</a>");

			result.append("<script type='text/javascript'>\n");
			result.append("function goPage(pageCount, query){\n");
			result.append("var pageIndex = document.getElementById(\"pageIndex\");\n");
			result.append("if(isNaN(pageIndex.value) || pageIndex.value.length == 0){\n");
			result.append("pageIndex.value = 1;\n");
			result.append("}\n");
			result.append("if(parseInt(pageIndex.value) > parseInt(pageCount)){\n");
			result.append("pageIndex.value = pageCount;\n");
			result.append("}\n");
			result.append("document.getElementById(\"a_go\").href = query + \"pageIndex=\"+document.getElementById(\"pageIndex\").value;\n");
			result.append("}\n");
			result.append("</script>");

		} catch (Exception e) {
			e.printStackTrace();
		}

		conditionsQuery = null;

		return result.toString();
	}

	/**
	 * 获得总页数
	 * 
	 * @return
	 */
	private int getPageCount() {
		if (pageSize == 0) {
			return 0;
		}
		if (itemCount % pageSize != 0) {
			return (int) itemCount / pageSize + 1;
		}
		return (int) itemCount / pageSize;
	}

	public void setItemCount(long itemCount) {
		this.itemCount = itemCount;
	}

	public void setPageIndex(long pageIndex) {
		this.pageIndex = pageIndex;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public void setConditions(String conditions) {
		this.conditions = conditions;
	}

	public void setQueryValues(String queryValues) {
		this.queryValues = queryValues;
	}

	public void setIndexClass(String indexClass) {
		this.indexClass = indexClass;
	}

}
